import re

# 1词云显示-----实现===ok===
# 2综合：默认列表前三十的，--实现---柱形图列出前十；===OK===
# 销量(按照评论数随机加100w级别排序)，名称，简介，价格，销量（评论数+100W上下）柱形图正常===
# 评论数列表，名称，评论数，好评数，追评数，晒单数，差评数，图柱状图正常
# 分析：最爱的价格范围（饼图，总的分析），好评率和销量关系（双轴图：柱状图+折线图）
# 文件栏：只有保存功能编辑栏不做开发，窗口栏不做，显示可以设置（透明度啊啥的）语言新增
# 一个窗口设中英对照，帮助弹出，其他控件不做
import random


def JD_ciyun():
    #词云
    with open('JD_lingshi_ceshi.csv', encoding='utf-8') as JD:
        JD.seek(0)
        list_jd = re.split('[,\n. ]', JD.read(100000))
        #print(list_jd)
        string_jd = ' '.join(list_jd)
        string_jd.split(' ')
        #print(string_jd)
        pa = re.compile(r'[\u4e00-\u9fa5]+')
        ch_list_jd = pa.findall(string_jd)
        #print(ch_list_jd)
        ch_str_jd = ' '.join(ch_list_jd)
        #print(ch_str_jd)
        dic_num = {}
        for i in ch_list_jd:
            dic_num[i] = ch_list_jd.count(i)
        #print(dic_num)
        last = sorted(dic_num.items(),key=lambda x:x[1],reverse=True)
        #print(last)
        return last

def zonghe():
    #前十，好评数和销量
    ## 2综合：默认列表前三十的，--实现---柱形图列出前十；
    name = []
    xiaoliang = []
    goodt = []
    with open('JD_lingshi_2.csv', encoding='utf-8') as JD:
        JD.seek(0)
        String_jd = re.split('[,\n]', JD.read(1000))
        for i in range(len(String_jd)):
            if i%12==0:
                name.append(String_jd[i])
            elif i%12==3:
                goodt.append(int(String_jd[i]))
                ture = int(String_jd[i]) + random.randint(100000,1000000)
                xiaoliang.append(ture)
        return [name,xiaoliang,goodt]

def xiaoliang(zonghe):
    # 销量(按照评论数随机加100w级别排序)，名称，简介，价格，销量（评论数+100W上下）柱形图正常
    jiage = []
    jianjie = []
    new_dict = {}
    with open('JD_lingshi_2.csv', encoding='utf-8') as JD:
        JD.seek(0)
        String_jd = re.split('[,\n]', JD.read(1000))
        for i in range(len(String_jd)):
            if i%12==1:
                jianjie.append(String_jd[i])
            elif i%12==2:
                jiage.append(float(String_jd[i]))

        for x in range(min(len(zonghe[0]),len(jianjie),len(jiage))):
            a= []
            a.append(zonghe[0][x])
            a.append(jianjie[x])
            a.append(jiage[x])
            new_dict[str(a)] = zonghe[1][x]
    #销量
    last_list = sorted(new_dict.items(), key=lambda x:x[1] , reverse=True)
    return last_list

def pinglun():
    # 评论数列表，名称0，评论数3，好评数5，追评数8，晒单数9，差评数10，图柱状图正常

    name = []
    CommentCount = []
    GoodCount = []
    AfterCount = []
    VideoCount = []
    PoorCount = []
    with open('JD_lingshi_2.csv', encoding='utf-8') as JD:
        JD.seek(0)
        String_jd = re.split('[,\n]', JD.read(1000))
        for i in range(len(String_jd)):
            if i % 12 == 0:
                name.append(String_jd[i])
            elif i % 12 == 3:
                CommentCount.append(int(String_jd[i]))
            elif i % 12 == 5:
                GoodCount.append(int(String_jd[i]))
            elif i % 12 == 8:
                AfterCount.append(int(String_jd[i]))
            elif i % 12 == 9:
                VideoCount.append(int(String_jd[i]))
            elif i % 12 == 10:
                PoorCount.append(int(String_jd[i]))
    return [name,CommentCount,GoodCount,AfterCount,VideoCount,PoorCount]

def price_best():
    # 分析：最爱的价格范围（饼图，总的分析），好评率和销量关系（双轴图：柱状图+折线图）
    price = []
    goodc = []
    xiaoliang = []
    p_20 = 0
    p_50 = 0
    p_100 = 0
    p_200 = 0
    p_other = 0
    with open('JD_lingshi_2.csv', encoding='utf-8') as JD:
        JD.seek(0)
        String_jd = re.split('[,\n]', JD.read(100000))
        for i in range(len(String_jd)):
            if i % 12 == 2:
                price.append(String_jd[i])
            elif i % 12 == 3:
                goodc.append(int(String_jd[i]))
                ture = int(String_jd[i]) + random.randint(100000, 1000000)
                xiaoliang.append(ture)
    for x in range(min(len(price),len(xiaoliang))):
        if float(price[x])<20:
            p_20 = p_20 + xiaoliang[x]
            continue
        elif float(price[x])<50:
            p_50 = p_50 + xiaoliang[x]
            continue
        elif float(price[x])<100:
            p_100 = p_100 + xiaoliang[x]
            continue
        elif float(price[x])<200:
            p_200 = p_200 + xiaoliang[x]
            continue
        else:
            p_other = p_other + xiaoliang[x]
    return zip(['小于20元','20-50元','50-100元','100-200元','大于200元'],[p_20,p_50,p_100,p_200,p_other])

def fx_last():
    name = []
    xiaoliang = []
    haopinglv = []
    zongpingshu = []
    with open('JD_lingshi_2.csv', encoding='utf-8') as JD:
        JD.seek(0)
        String_jd = re.split('[,\n]', JD.read(1000))
        for i in range(len(String_jd)):
            if i % 12 == 0:
                name.append(String_jd[i])
            elif i % 12 == 3 or i % 12 == 5:
                if i % 12 == 3:
                    zongpingshu = float(String_jd[i])
                    ture = int(String_jd[i]) + random.randint(100000, 1000000)
                    xiaoliang.append(ture)

                else:

                    haopinglv.append((float(String_jd[i]))/zongpingshu)
                    zongpingshu = []

        return [name, xiaoliang,haopinglv]